﻿using GuanQL.Core.Domain.Accounts;
using GuanQL.Core.Stores;
using GuanQL.DataMigration.Seeds;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using Zhongjia.Zero.DataMigration;
using ZhongJia.Core;

namespace GuanQL.DataMigration
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddCommandLine(args)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            var configuration = builder.Build();

            var services = new ServiceCollection()
                         .AddLogging()
                         .AddSingleton<IDatabaseInitializer, DatabaseInitializer>()
                         .AddEntityFramework()
                           .AddDbContext<GQLDbContext>(option =>
                           {
                               option.UseSqlServer(configuration["conn"]);
                           });

            services.AddIdentity<User, Role>()
                                .AddRoleStore<GQLRoleStore>()
                                .AddUserStore<GQLUserStore>()
                                  .AddDefaultTokenProviders();

            services.Configure<IdentityOptions>(options =>
            {
                options.User.AllowedUserNameCharacters = null;
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequireLowercase = false;
            });

            services.AddScoped<ISeeder, PictureSeed>();
            services.AddScoped<ISeeder, UserSeed>();
            services.AddScoped<ISeeder, TenantSeed>();

            var serviceProvider = services.BuildServiceProvider();
            var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
            loggerFactory.AddConsole(minLevel: LogLevel.Information);

            var databaseInitializer = serviceProvider.GetService<IDatabaseInitializer>();
            databaseInitializer.Seed().Wait();
            Console.WriteLine("数据生成成功");
        }
    }
}